package com.css.fxfzypg.modules.SettingSeismic.repository.impl;

import com.css.fxfzypg.modules.SettingSeismic.entity.SeismicIntensityRelation;
import com.css.fxfzypg.modules.SettingSeismic.entity.SettingSeismicProcess;
import com.css.fxfzypg.modules.SettingSeismic.repository.IntensityRelationRepository;
import com.css.fxfzypg.modules.model.entity.*;
import com.css.fxfzypg.util.JdbcUtil;
import com.css.fxfzypg.util.PlatformObjectUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: fxfzypg
 * @Author: lhl
 * @CreateDate: 2022/10/20 14:28
 */
@Repository
@Slf4j
public class IntensityRelationRepositoryImpl implements IntensityRelationRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Object getModelByTaskId(String taskId, String type) {
        String sql = "select a.*";
        switch (type) {
            case "1":
                sql = sql + " from intensity_attenuation a";break;
            case "2":
                sql = sql + " from casualty_calculation a";break;
            case "3":
                sql = sql + " from economic_losses a";break;
            case "4":
                sql = sql + " from resettlement_personnel a";break;
            case "5":
                sql = sql + " from rescue_force_assessment a";break;
            case "6":
                sql = sql + " from material a";break;
        }
        sql = sql + " left join seismic_intensity_relation b";
        switch (type) {
            case "1":
                sql = sql + " on a.id=b.model1_id";break;
            case "2":
                sql = sql + " on a.id=b.model2_id";break;
            case "3":
                sql = sql + " on a.id=b.model3_id";break;
            case "4":
                sql = sql + " on a.id=b.model4_id";break;
            case "5":
                sql = sql + " on a.id=b.model5_id";break;
            case "6":
                sql = sql + " on a.id=b.model6_id";break;
        }
        sql = sql + " where a.is_valid='1' and b.is_delete='0' and b.task_id='" + taskId + "'";
        try {
            switch (type) {
                case "1":
                    return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(IntensityAttenuation.class));
                case "2":
                    return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Casualty.class));
                case "3":
                    return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(EconomicLosses.class));
                case "4":
                    return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(ResettlementPersonnel.class));
                case "5":
                    return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(RescueForceEntity.class));
                case "6":
                    return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Material.class));
            }
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public SettingSeismicProcess getProcessByTaskId(String taskId) {
        String sql = "select a.* from setting_seismic_process a" +
                " left join seismic_intensity_relation b on a.id=b.model1_id" +
                " where a.is_delete='0' and b.is_delete='0' and b.task_id='" + taskId + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SettingSeismicProcess.class));
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public SeismicIntensityRelation getByTaskId(String taskId) {
        String sql = "select b.* from  seismic_intensity_relation b  where b.is_delete='0' and b.task_id='" + taskId + "'";
        try {
            //return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SeismicIntensityRelation.class));
            List<SeismicIntensityRelation> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SeismicIntensityRelation.class));
            if(PlatformObjectUtils.isNotEmpty(query) && query.size()>0){
                return query.get(0);
            }
        } catch (Exception e) {
        }
        return null;
    }

    @Override
    public List<SeismicIntensityRelation> getAll() {
        String sql = "select b.* from  seismic_intensity_relation b  where b.is_delete='0'";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(SeismicIntensityRelation.class));

        }

    @Override
    public int saveSeismicIntensityRelation(SeismicIntensityRelation entity) {

        String sql = "insert into seismic_intensity_relation(id,task_id,model1_id,model1_type,is_delete,extends1," +
                "extends2,model2_id,model3_id,model4_id,model4_type,model5_id,model6_id,model7_id,model7_type," +
                "button1_status1,button2_status1,button2_status2,button2_status3,button3_status1,button3_status2," +
                "button4_status1,button4_status2,button5_status1,button6_status1,button7_status1,button8_status1," +
                "button8_status2,button8_status3,button9_status1)" +
                " values(:id,:taskId,:model1Id,:model1Type,:isDelete,:extends1,:extends2,:model2Id," +
                ":model3Id,:model4Id,:model4Type,:model5Id,:model6Id,:model7Id,:model7Type," +
                ":button1_status1,:button2_status1,:button2_status2,:button2_status3,:button3_status1,:button3_status2," +
                ":button4_status1,:button4_status2,:button5_status1,:button6_status1,:button7_status1,:button8_status1," +
                ":button8_status2,:button8_status3,:button9_status1)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return i;
    }

    @Override
    public int saveSettingSeismicProcess(SettingSeismicProcess entity) {
        String sql = "insert into setting_seismic_process(id,epicenter_intensity,long_length1,short_length1," +
                "long_length2,short_length2,long_length3,short_length3,long_length4,short_length4,long_length5," +
                "short_length5,create_user_id,create_user,is_delete,create_time)" +
                " values(:id,:epicenterIntensity,:longLength1,:shortLength1,:longLength2,:shortLength2,:longLength3," +
                ":shortLength3,:longLength4,:shortLength4,:longLength5,:shortLength5,:createUserId,:createUser," +
                ":isDelete,:createTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return i;
    }

    @Override
    public int updateSeismicIntensityRelation(SeismicIntensityRelation entity) {
        String sql = "update seismic_intensity_relation set task_id=:taskId,model1_id=:model1Id,model1_type=:model1Type," +
                "is_delete=:isDelete,extends1=:extends1,extends2=:extends2,model2_id=:model2Id,model3_id=:model3Id," +
                "model4_id=:model4Id,model4_type=:model4Type,model5_id=:model5Id,model6_id=:model6Id,model7_id=:model7Id," +
                "model7_type=:model7Type,button1_status1=:button1_status1,button2_status1=:button2_status1," +
                "button2_status2=:button2_status2,button2_status3=:button2_status3,button3_status1=:button3_status1," +
                "button3_status2=:button3_status2,button4_status1=:button4_status1,button4_status2=:button4_status2," +
                "button5_status1=:button5_status1,button6_status1=:button6_status1,button7_status1=:button7_status1," +
                "button8_status1=:button8_status1,button8_status2=:button8_status2,button8_status3=:button8_status3," +
                "button9_status1=:button9_status1" +
                " where id=:id and is_delete='0'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(entity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return i;
    }

    /*@Override
    public int deleteSsiByTaskId(String taskId) {
        String sql = "delete from setting_seismic_influenceflied where task_id='" + taskId + "'";
        return jdbcTemplate.update(sql);
    }*/

    @Override
    public int deleteSsiByTaskId(String taskId) {
//        String sql = "drop table setting_seismic_influenceflied_" + taskId;
        log.info("删除分区表开始-------------");
        String sql = "select DropGeometryTable('setting_seismic_influenceflied_"+taskId+"')";//空间表删除
        JdbcUtil.executeSql(sql);
        log.info("删除分区表结束-------------");
       return 0;
    }

    @Override
    public int deleteSipByTaskId(String taskId) {
        String sql = "delete from seismic_influencefly_pbe where task_id='" + taskId + "'";
        return jdbcTemplate.update(sql);
    }

    @Override
    public SettingSeismicProcess getProcessById(String id) {
        String sql = "select b.* from  setting_seismic_process b  where b.is_delete='0' and id='"+id+"'";
        return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(SettingSeismicProcess.class));

    }

    @Override
    public void updateSeismicIntensityRelationByTaskId(String id) {
        String sql = "update seismic_intensity_relation set button1_status1='0',button2_status1='0', button2_status2='0',button2_status3='0',button3_status1='0', button3_status2='0',button4_status1='0',button4_status2='0', button5_status1='0',button6_status1='0',button7_status1='0',button8_status1='0',button8_status2='0',button8_status3='0', button9_status1='0' where id='"+id+"' and is_delete='0'";
        jdbcTemplate.update(sql);
    }
}
